package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.dto.SalesSignDto;
import com.prd.mapper.EntContactForeignMapper;
import com.prd.model.*;
import com.prd.service.*;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import com.prd.vo.EntContactForeignVo;
import com.prd.vo.UserBaseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntContactForeignServiceImpl extends ServiceImpl<EntContactForeignMapper, EntContactForeign> implements EntContactForeignService {

    @Autowired
    private EntContactForeignMapper entContactForeignMapper;

    @Autowired
    private EntContactForeignCheckService entContactForeignCheckService;

    @Autowired
    private EntContactForeignAcceptorService entContactForeignAcceptorService;

    @Autowired
    private UserService userService;

    @Autowired
    private EntContactForeignSignService entContactForeignSignService;

    @Override
    public int insertInfo(EntContactForeign entContactForeign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entContactForeign);
        entContactForeign.setNumber(String.valueOf(System.nanoTime()));
        entContactForeign.setStatus(CheckStatus.ONE.getCode());
        return entContactForeignMapper.insert(entContactForeign);
    }

    @Override
    public int updateInfo(EntContactForeign entContactForeign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entContactForeign);
        return entContactForeignMapper.updateById(entContactForeign);
    }

    @Override
    public EntContactForeignVo details(Long id) {
        EntContactForeignVo entContactForeignVo = new EntContactForeignVo();
        EntContactForeign entContactForeign = entContactForeignMapper.selectById(id);
        if (entContactForeign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entContactForeign, entContactForeignVo);

        //获取用户信息
        List<Long> arrayList = new ArrayList<>();
        List<Long> makes = Arrays.asList(StringsUtils.convert(entContactForeign.getMake().split(",")));
        arrayList.addAll(makes);
        arrayList.add(StringsUtils.isNotBlank(entContactForeign.getInitiator()) ? 0 : Long.valueOf(entContactForeign.getInitiator()));
        arrayList.stream().distinct().collect(Collectors.toList());
        List<UserBaseVo> listUser = userService.listName(arrayList.toArray(new Long[arrayList.size()]));
        List<String> listMake = new ArrayList<>();
        for (UserBaseVo userBaseVo : listUser) {
            if (entContactForeign.getInitiator().equals(userBaseVo.getId().toString())) {
                entContactForeignVo.setInitiator(userBaseVo.getMerge());
            }
            if (makes.contains(userBaseVo.getId())) {
                listMake.add(userBaseVo.getMerge());
            }
        }
        entContactForeignVo.setMakes(listMake);

        LambdaQueryWrapper<EntContactForeignAcceptor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntContactForeignAcceptor::getForeignId, id);
        entContactForeignVo.setAcceptors(entContactForeignAcceptorService.list(queryWrapper));

        LambdaQueryWrapper<EntContactForeignCheck> queryWrapperCheck = new LambdaQueryWrapper<>();
        queryWrapperCheck.eq(EntContactForeignCheck::getForeignId, id);
        entContactForeignVo.setChecks(entContactForeignCheckService.list(queryWrapperCheck));

        LambdaQueryWrapper<EntContactForeignSign> queryWrapperSign = new LambdaQueryWrapper<>();
        queryWrapperSign.eq(EntContactForeignSign::getForeignId, id);
        entContactForeignVo.setSigns(entContactForeignSignService.list(queryWrapperSign));
        return entContactForeignVo;
    }

    @Override
    public List<EntContactForeign> listInfo(String day, Integer catalog) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntContactForeign> queryWrapper = new LambdaQueryWrapper<>();
        //全部
        if (catalog == 0) {

        } else if (catalog == 1) {//我发起
            queryWrapper.eq(EntContactForeign::getCreateId, loginUser.getId());
        } else if (catalog == 2) {//我接收
            queryWrapper.eq(EntContactForeign::getInitiator, loginUser.getId());
            queryWrapper.or().apply("find_in_set({0},make)", loginUser.getId());
        } else if (catalog == 3) {//我制单
            queryWrapper.eq(EntContactForeign::getCreateId, loginUser.getId());
        } else if (catalog == 4) {//待确认
            queryWrapper.eq(EntContactForeign::getAffirm, 1);
        } else if (catalog == 5) {//已确认
            queryWrapper.eq(EntContactForeign::getAffirm, 2);
        } else if (catalog == 6) {//待审批
            queryWrapper.eq(EntContactForeign::getStatus, CheckStatus.ONE.getCode());
        } else if (catalog == 7) {//审批通过
            queryWrapper.eq(EntContactForeign::getStatus, CheckStatus.TWO.getCode());
        } else if (catalog == 8) {//审批不通过
            queryWrapper.eq(EntContactForeign::getStatus, CheckStatus.THREE.getCode());
        } else {
            throw new RuntimeException("找不到类型！");
        }
        List<EntContactForeign> list = entContactForeignMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public int pass(EntContactForeignCheck entContactForeignCheck) {
        EntContactForeign entContactForeign = entContactForeignMapper.selectById(entContactForeignCheck.getForeignId());
        if (entContactForeign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entContactForeign.setStatus(entContactForeignCheck.getType());
        entContactForeignCheckService.insertInfo(entContactForeignCheck);
        return updateInfo(entContactForeign);
    }

    @Override
    public int addMake(Long foreignId, Long[] makes) {
        EntContactForeign entContactForeign = entContactForeignMapper.selectById(foreignId);
        if (entContactForeign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        if (StringUtils.isNotBlank(entContactForeign.getMake())) {
            entContactForeign.setMake(entContactForeign.getMake() + "," + makes.toString());
        } else {
            entContactForeign.setMake(makes.toString());
        }
        return updateInfo(entContactForeign);
    }

    @Override
    public int salesSign(SalesSignDto salesSignDto) {
        EntContactForeign entContactForeign = entContactForeignMapper.selectById(salesSignDto.getForeignId());
        if (entContactForeign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(salesSignDto, entContactForeign);
        entContactForeign.setSignTime(new Date());
        entContactForeign.setAffirm(1);
        return updateInfo(entContactForeign);
    }

    @Override
    public int acceptorSign(EntContactForeignSign entContactForeignSign) {
        EntContactForeign entContactForeign = entContactForeignMapper.selectById(entContactForeignSign.getForeignId());
        if (entContactForeign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entContactForeignSignService.insertInfo(entContactForeignSign);
        entContactForeign.setAffirm(2);
        return updateInfo(entContactForeign);
    }
}
